package org.example.k8s_cloudplatform.utils;

import io.kubernetes.client.openapi.ApiClient;

import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
import org.example.k8s_cloudplatform.entity.Pod;
import org.example.k8s_cloudplatform.entity.PodRequest;

import java.util.Calendar;
import java.util.Date;


public class K8S_util {
    public static final ApiClient client= getApiClient();
    public static final CoreV1Api coreApi=new CoreV1Api(client);

    public static ApiClient getApiClient(){

        String master = "https://192.168.88.137:6443";
        String authToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6Im1jZHlTcU1RTEZ5dkJkd1cwWFpYaUpyWFVhYmhKdy1sUWNfdXFuaDhwWG8ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi1zZWNyZXQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI4OWM5NjQ5YS02M2ExLTQxMTAtYWViZS1mNzJiMmE5YjIwMTgiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4ifQ.F775KdIrC77yz65c2jV7o2IW5ru5Km8h9k3uBj1kEZWWf1JlpmXtW2tLEAZkjpvsH85FXkAjqqEYd8NwpfSCGy-1XeGWP72NqmCgTuc4UPfXPH4okwVqOspHQa4mp5KToZvj69Pvp02jr_NUyaM7rBX-80qBcD2S6eIKiaF8mn6LBwCL1k1ewS1iv2Pu5duti5y-7YqzZFhoZ-atY8f21TGkIRH8894lHJfe_H2SuHsHUS23ncIvxaClo3_M8xZAgp976sgysnFIEP2tfKlA3dag6FTkfnn1BylFK--R0A6g5uTbNL6EnHYft2PZ8RWGFFr191nvRXN4maPUKh2hrQ";

        ApiClient apiClient = new ClientBuilder()
                //设置 k8s 服务所在 ip地址
                .setBasePath(master)
                //是否开启 ssl 验证
                .setVerifyingSsl(false)
                //插入访问连接用的 Token
                .setAuthentication(new AccessTokenAuthentication(authToken))
                .build();
        io.kubernetes.client.openapi.Configuration.setDefaultApiClient(apiClient);
        return apiClient;
    }

    public static Pod toPod(PodRequest podRequest){
        Pod pod=new Pod();
        pod.setName(podRequest.getPodName());
        pod.setUserID(podRequest.getUserID());
        pod.setCpu(podRequest.getCpu());
        pod.setMemory(podRequest.getMemory());
        pod.setImageOS(podRequest.getImageOS());
        pod.setImageVersion(podRequest.getImageVersion());
        pod.setStatus(0);//默认激活

        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DAY_OF_MONTH, podRequest.getApplyTime());
        pod.setExpirationTime(calendar.getTime());

        return pod;
    }

    public static PodRequest toPodRequest(Pod pod){
        PodRequest podRequest=new PodRequest();
        podRequest.setUserID(pod.getUserID());
        podRequest.setPodName(pod.getName());
        podRequest.setCpu(pod.getCpu());
        podRequest.setMemory(pod.getMemory());
        podRequest.setImageOS(pod.getImageOS());
        podRequest.setImageVersion(pod.getImageVersion());

        return podRequest;
    }
    public static boolean isValidPodName(String podName) {
        // 检查字符串是否为空或长度为0
        if (podName == null || podName.isEmpty()) {
            return false;
        }
        // 检查字符串是否以字母数字字符开头
        if (!Character.isLowerCase(podName.charAt(0)) && !Character.isDigit(podName.charAt(0))) {
            return false;
        }
        // 检查字符串是否以字母数字字符结尾
        if (!Character.isLowerCase(podName.charAt(podName.length() - 1)) && !Character.isDigit(podName.charAt(podName.length() - 1))) {
            return false;
        }
        // 检查字符串中的每个字符是否都满足条件
        for (int i = 1; i < podName.length() - 1; i++) {
            char c = podName.charAt(i);
            if (!Character.isLowerCase(c) && !Character.isDigit(c) && c != '-' && c != '.') {
                return false;
            }
        }
        // 如果所有检查都通过，则返回true
        return true;
    }


}
